package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsArray;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * servers
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerServer extends Segment {
    private String $url;
    private String $desc;

    public SwaggerServer(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $url = PropertiesConstants.getRaw(p,"url",$url);
        $desc = PropertiesConstants.getRaw(p,"description",$desc);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        XsArray array = current.getArrayChild("servers",true);
        if (array != null){
            XsObject newObject = array.newObject();
            newObject.addProperty("description",PropertiesConstants.transform(ctx,$desc,""));
            newObject.addProperty("url",PropertiesConstants.transform(ctx,$url,""));
            super.onExecute(root,newObject,ctx,watcher);
            array.add(newObject);
        }
    }
}